﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;


namespace BT_web__nhom06.BiThuChiDoan
{
    public partial class PhanCongHoatDong : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            string mahoatdong = dlstmahoatdong.SelectedValue.ToString();
            string machidoan = dlstMaChiDoan.SelectedValue.ToString();

            //if (IsPostBack == false)
            //{
            //    string ch = "SELECT dv.MaSinhVien, [Ho], [Ten], [VaiTro], [DanhGia] FROM [DoanVien] dv,[PhanCongHoatDongDoanVien] pc WHERE (dv.MaSinhVien = pc.MaSinhVien)";             
            //    DataTable tbl2 = Tienichsql.ketnoicsdl.ChayLenhSelectDuLieu(ch);
            //    gvDoanVien2.DataSource = tbl2;
            //    gvDoanVien2.DataBind();
            //}

        }

        protected void dlstmahoatdong_SelectedIndexChanged(object sender, EventArgs e)
        {
            string mahoatdong = dlstmahoatdong.SelectedValue.ToString();
            txt_tenhoatdong.Text = mahoatdong;
            string lenhsql = "select tenhoatdong from hoatdongdoan where mahoatdong = @mahoatdong";
            DataTable tbl = Tienichsql.ketnoicsdl.ChayLenhSelectDuLieu(lenhsql, "@mahoatdong", mahoatdong);
            DataRow dr = tbl.Rows[0];
            txt_tenhoatdong.Text = (string)dr["tenhoatdong"];
            capnhatdanhsach();

        }

        protected void dlstmahoatdong_TextChanged(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {

        }

        protected void dlstMaChiDoan_SelectedIndexChanged(object sender, EventArgs e)
        {
            string machidoan = dlstMaChiDoan.SelectedValue.ToString();
            lblTenChiDoan.Text = machidoan;
            string lenhsql = "select machidoan,tenchidoan from chidoan where machidoan = @machidoan";
            DataTable tbl = Tienichsql.ketnoicsdl.ChayLenhSelectDuLieu(lenhsql, "@machidoan", machidoan);
            DataRow dr = tbl.Rows[0];
            lblTenChiDoan.Text = (string)dr["tenchidoan"];
            capnhatdanhsach();
        }

        public void capnhatdanhsach()
        {
            string Smachidoan = dlstMaChiDoan.SelectedValue.ToString();
            string Smahoatdong = dlstmahoatdong.SelectedValue.ToString();

            string lenhSelectSQLDaPhanCong = "SELECT dv.MaSinhVien, [Ho], [Ten], [VaiTro], [DanhGia] FROM [DoanVien] dv,[PhanCongHoatDongDoanVien] pc WHERE ((dv.MaSinhVien = pc.MaSinhVien) AND ([MaChiDoan] = " + Smachidoan + ") AND ([MaHoatDong] = " + Smahoatdong + "))";
            DataTable tblDaPhanCong = Tienichsql.ketnoicsdl.ChayLenhSelectDuLieu(lenhSelectSQLDaPhanCong);
            gvDuocPhanCong.DataSource = tblDaPhanCong;
            gvDuocPhanCong.DataBind();

            //TODO tim ra nhung doan vien chua duoc phan cong ung voi chidoan va hoat dong 
            //SELECT MaSinhVien, Ho, Ten 
            //FROM DoanVien 
            //WHERE (MaChiDoan =001) 
            //and
            //MaSinhVien  NOT IN 
            //(SELECT dv.MaSinhVien FROM [DoanVien] dv,[PhanCongHoatDongDoanVien] pc 
            //WHERE (dv.MaSinhVien = pc.MaSinhVien) AND ([MaHoatDong] = 131201))

            string lenhSelectSQLChuaPhanCong = "SELECT MaSinhVien, Ho, Ten FROM DoanVien WHERE (MaChiDoan =" + Smachidoan + ") and MaSinhVien  NOT IN (SELECT dv.MaSinhVien FROM [DoanVien] dv,[PhanCongHoatDongDoanVien] pc WHERE (dv.MaSinhVien = pc.MaSinhVien) AND ([MaHoatDong] = " + Smahoatdong + "))";
            //string lenhSelectSQLChuaPhanCong = "SELECT MaSinhVien, Ho, Ten FROM DoanVien WHERE (MaChiDoan ='" + Smachidoan + ")";       
            DataTable tblChuaPhanCong = Tienichsql.ketnoicsdl.ChayLenhSelectDuLieu(lenhSelectSQLChuaPhanCong);
            gvDVChuaPhanCong.DataSource = tblChuaPhanCong;
            gvDVChuaPhanCong.DataBind();



        }

        protected void btnApDungPhanCong_Click(object sender, EventArgs e)
        {
            //       if (GridView1.Rows.Count>0)
            //       {
            //           int ketqua = 0;

            //           foreach (GridViewRow row in GridView1.Rows)
            //           { 
            //               CheckBox check = (CheckBox)row.FindControl("DuocPhanCong");
            //               if (check.Checked)
            //               {
            ////                   string lenhsql = "insert into [phanconghoatdongdoanvien]([masinhvien],[MaHoatDong],[VaiTro],[DanhGia]) values(@masinhvien,@MaHoatDong,@VaiTro,@DanhGia)";
            ////                   ketqua = Tienichsql.ketnoicsdl.chaylenhcapnhatdulieu(lenhsql, new string[] { "@masinhvien", "@MaHoatDong", "@VaiTro", "@DanhGia"}, new object[] { masinhvien, MaHoatDong, VaiTro, DanhGia});
            //               }


            //               //EX!!!
            //               //IF AccessType = 1
            //               //{
            //               //    check.Checked = true;
            //               //}

            //               //IF AccessType = 2
            //               //{
            //               //    check2.Checked = true;
            //               //}
            //           }
            //       }
        }




    }
}